/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * @flow
 */

import React, {Component} from 'react';
import {
    AppRegistry,
    AppState,
    View,
} from 'react-native';

// 项目结构
// 1：首页【商品 ，店铺模块】
// 2：订单模块
// 3：我的设置相关模块
// 4：水票模块

// import App from './js/App'
import AppLuo from './luojs/AppLuo';

import codePush from 'react-native-code-push'
import {Loading, EasyLoading} from 'react-native-easy-loading';
console.disableYellowBox = true;
const codePushOptions = {checkFrequency: codePush.CheckFrequency.MANUAL};

class Entry extends Component {

    componentDidMount() {
        //监听状态改变事件
        AppState.addEventListener('change', this.handleAppStateChange.bind(this));

        LOG('Entry##')
    }

    componentWillUnmount() {
        //删除状态改变事件监听
        AppState.removeEventListener('change', this.handleAppStateChange.bind(this));
    }

    //状态改变响应
    handleAppStateChange = (appState) =>{
        if(appState == 'active'){
            this._checkUpdate();
        }
    }

    // 检测版本更新
    _checkUpdate() {
        codePush.checkForUpdate().then(update => {
            if (update) {
                codePush.sync({
                        updateDialog: false,
                        installMode: codePush.InstallMode.IMMEDIATE
                    },
                    (status) => {
                        switch (status) {
                            case codePush.SyncStatus.DOWNLOADING_PACKAGE:
                                EasyLoading.show('开始更新...');
                                break;
                            case codePush.SyncStatus.INSTALLING_UPDATE:
                                EasyLoading.dismis();
                                break;
                        }
                    },
                    (progress) => {
                        let downloadPercent = progress.receivedBytes/progress.totalBytes*100 ;
                        EasyLoading.show('更新进度' + downloadPercent.toFixed(0)+'%');
                    });
            }
        })
    }

    render() {
        return <View style={{flex: 1}}>
            <AppLuo/>
            <Loading/>
        </View>
    }
}

Entry = codePush(codePushOptions)(Entry);
AppRegistry.registerComponent('HBWater', () => Entry);
